#include "backlightSetWin.h"

////**6号***/

/*创建一个键盘事件组*/
static lv_group_t* keyGroup;
/*创建一个调节条*/
static lv_obj_t* slider_label;
/*记录旧的背光数据*/
static uint16_t old_backlight;

//当进度条的值改变时回调这个函数
static void slider_changeVal_cb(lv_event_t * e)
{
    lv_obj_t * slider = lv_event_get_target(e);
    int32_t val = lv_slider_get_value(slider);
    lv_label_set_text_fmt(slider_label,"%d%%",val);
    LCD_Set_Brilliance((uint16_t)(val*10));
}

/**退出本界面回调函数**/
static void exit_screen(void* scr)
{
	lv_obj_del(scr);
}

/**"Enter"按键回调函数**/
static void enter_btn_cb(lv_event_t * e)
{
	//保存并退出
    //在改变值回调中已经设置完成，这里直接退出即可，无需保存
	lv_group_del(keyGroup);
	lv_group_set_default(NULL);
	lv_async_call(exit_screen, lv_scr_act());
	menu(6);
}

/**"Cancel"按钮回调函数**/
static void cancel_btn_cb(lv_event_t * e)
{
    LCD_Set_Brilliance(old_backlight);
	lv_group_del(keyGroup); //不保存退出
	lv_group_set_default(NULL);
	lv_async_call(exit_screen, lv_scr_act());
	menu(6);
}


void open_backlightSetWin()
{
	lv_obj_t*  scr = lv_obj_create(NULL);
	lv_scr_load(scr);

    lv_obj_t* label_title = lv_label_create(lv_scr_act()); //标题标签
	lv_obj_set_width(label_title, 160);
	lv_obj_set_style_text_font(label_title, &yahei_16, 0);
	lv_obj_add_style(label_title, &style_label_3, LV_PART_MAIN);
	lv_obj_align(label_title, LV_ALIGN_TOP_MID, 0, 0);
	lv_label_set_text(label_title, "背光设置");

    lv_obj_t*slider = lv_slider_create(lv_scr_act());
    lv_obj_add_event_cb(slider,slider_changeVal_cb,LV_EVENT_VALUE_CHANGED,NULL);
    lv_obj_align(slider,LV_ALIGN_CENTER,-22,0);
    lv_obj_set_width(slider,100);
    lv_slider_set_range(slider, 5, 100);
    old_backlight = LCD_get_Brilliance();
    lv_slider_set_value(slider,old_backlight/10,LV_ANIM_OFF);
    
    slider_label = lv_label_create(lv_scr_act());
    //lv_label_set_text(slider_label, "0%");
    lv_label_set_text_fmt(slider_label,"%d%%",old_backlight/10);
    lv_obj_align_to(slider_label, slider, LV_ALIGN_OUT_RIGHT_MID, 10, 0);

    keyGroup = lv_group_create();
	lv_indev_set_group(indev_keypad, keyGroup);
	lv_group_set_default(keyGroup);

	lv_obj_t* btn1 = lv_btn_create(lv_scr_act());
	lv_obj_t* label = lv_label_create(btn1);
	lv_label_set_text(label, "Enter");
	lv_obj_align_to(label, btn1, LV_ALIGN_CENTER, 0, -5);
	lv_obj_set_size(btn1, LV_SIZE_CONTENT, 16);
	lv_obj_align(btn1, LV_ALIGN_BOTTOM_MID, -40, -3);
	lv_obj_add_event_cb(btn1, enter_btn_cb, LV_EVENT_PRESSED, NULL);

	lv_obj_t* btn2 = lv_btn_create(lv_scr_act());
	label = lv_label_create(btn2);
	lv_label_set_text(label, "Cancel");
	lv_obj_align_to(label, btn2, LV_ALIGN_CENTER, 0, -5);
	lv_obj_set_size(btn2, LV_SIZE_CONTENT, 16);
	lv_obj_align(btn2, LV_ALIGN_BOTTOM_MID, 40, -3);
	lv_obj_add_event_cb(btn2, cancel_btn_cb, LV_EVENT_PRESSED, NULL);

    lv_group_add_obj(keyGroup, slider);
	lv_group_add_obj(keyGroup, btn1);
	lv_group_add_obj(keyGroup, btn2);
}

